//
//  BuyersListTableViewController.m
//  FreightAssistant
//
//  Created by leo on 15/10/27.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "CustomerListTableViewController.h"

#import "CustomerEditViewController.h"

static NSString *cell_Identifier = @"tableViewCell";

@interface CustomerListTableViewController () {
    
    KKSearchBar *_kk_searchBar;
    
    KKCustomerItemList *_customerItemList;
}

@end

@implementation CustomerListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"客户信息";
    
    _kk_searchBar = [[KKSearchBar alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 40)];
    _kk_searchBar.placeholder = @"搜索";
    self.tableView.tableHeaderView = _kk_searchBar;
    self.tableView.tableFooterView = [UIView new];
    
    if (self.viewcontrollerType == KKViewControllerTypeEdit) {
        
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector(actionOfAdd:)];
        rightItem.tintColor = [UIColor whiteColor];
        
        self.navigationItem.rightBarButtonItem = rightItem;
        
        self.showMainItem = true;        
    }
    else {
        self.navigationItem.rightBarButtonItem = nil;
    }
    
 
    _customerItemList = [KKCustomerItemList new];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    
    [self downloadItemsFromDBWithSearchText: nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

- (void)downloadItemsFromDBWithSearchText:(NSString*)searchText {
    
    __weak typeof(_customerItemList) weakItemList = _customerItemList;
    __weak UITableView *weakTableView = self.tableView;
    
    [KKDataBaseAPIInterface searchCustomerItemsWithSearchText: searchText withEnabled: true successful:^(id object) {
        
        [weakItemList appendItemsWithArray: object];
        [weakTableView reloadData];
        
    } faile: nil];
}

- (void)deleteItemFromDBWithIndexPath:(NSIndexPath*)indexPath {
    
    KKCustomerItem *item = [_customerItemList objectAtIndex: indexPath.row];
    
    NSString *identifier = item.kk_identifier;
    if (identifier) {

        __weak typeof(_customerItemList) weakItemList = _customerItemList;
        __weak UITableView *weakTableView = self.tableView;
        
        [KKDataBaseAPIInterface deleteCustomerItemWithIdentifier: identifier successful:^(id object) {
            [weakItemList removeObjectOfIndex: indexPath.row];
            [weakTableView deleteRowsAtIndexPaths: @[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
        } faile: nil];
    }
}

#pragma mark - Action methods

- (void)actionOfAdd:(UIBarButtonItem*)item {
    
    CustomerEditViewController *editViewController = [CustomerEditViewController viewControllerFromStoryboard];
    [self.navigationController pushViewController: editViewController animated: true];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _customerItemList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cell_Identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: cell_Identifier];
    }
    
    KKCustomerItem *item = [_customerItemList objectAtIndex: indexPath.row];
    
    cell.textLabel.text = item.kk_name;
    cell.detailTextLabel.text = item.kk_phone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKCustomerItem *item = [_customerItemList objectAtIndex: indexPath.row];
    
    if (self.viewcontrollerType == KKViewControllerTypeChoose) {
        
        if (self.editResultBlock) {
            self.editResultBlock(item);
            
            [self.navigationController popViewControllerAnimated: true];
        }
    }
    else {
        
        CustomerEditViewController *editViewController = [CustomerEditViewController viewControllerFromStoryboard];
        editViewController.kk_customerItem = item;
        [self.navigationController pushViewController: editViewController animated: true];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.viewcontrollerType == KKViewControllerTypeChoose ? false : true;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        __weak typeof(self) weakSelf = self;
        [[KKAlertView shareInterce] showWithTitle: @"提示" andWithMessage: @"确定要删除这个客户的信息嘛?" andWithCancelTitle: @"取消" andWithCancelHandle: nil andWithSureTitle: @"确定" andWithSureHandle:^{
            
            [weakSelf deleteItemFromDBWithIndexPath: indexPath];
        }];
    }
}

@end
